using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Gravity : MonoBehaviour
{
    public static Gravity Instance;
    
    public List<Rigidbody2D> rigidbodies;
    
    
    public void Awake()
    {
        Instance = this;
        rigidbodies = new List<Rigidbody2D>();
    }

    // Start is called before the first frame update
    void Start()
    {
        rigidbodies.Add(PlayerController.Instance.GetComponent<Rigidbody2D>());
    }
    

    // Update is called once per frame
    void Update()
    {
        
    }

    public void OnTriggerEnter2D(Collider2D other)
    {
        Rigidbody2D rigidbody = other.GetComponent<Rigidbody2D>();
        if (rigidbody == null) return;
        foreach (Rigidbody2D rb in rigidbodies)
        {
            if (rigidbody.gameObject.name.Equals(rb.gameObject.name)) return;
        }
        rigidbodies.Add(rigidbody);
        
    }
    
    public void OnTriggerExit2D(Collider2D other)
    {
        foreach (Rigidbody2D rigidbody in rigidbodies)
        {
            if (rigidbody.gameObject.name.Equals(other.gameObject.name))
            {
                rigidbodies.Remove(rigidbody);
            }
        }
    }

    public void Enhance()
    {
        Debug.Log(2);
        foreach (Rigidbody2D rigidbody in rigidbodies)
        {
            rigidbody.gravityScale = Mathf.Clamp(rigidbody.gravityScale + 0.7f,-0.4f,1.7f);
            Debug.Log("执行");
        }
    }

    public void Weaken()
    {
        foreach (Rigidbody2D rigidbody in rigidbodies)
        {
            rigidbody.gravityScale = Mathf.Clamp(rigidbody.gravityScale - 0.7f,-0.4f,1.7f);
            Debug.Log("执行");
        }
    } 
    
    public void Destroy()
    {
        foreach (Rigidbody2D rigidbody in rigidbodies)
        {
            rigidbody.gravityScale = 1;
        }
        Destroy(this.gameObject);
    }
}
